import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import { getUBSPosition, getFssFundOptionsList } from '../../../../services/allposition_service';

export default {
  namespace: 'guo',
  state: {
    stocks: { code: 0, message: '', list: [] },
    stocksFilters: {},
    options: { code: 0, message: '', list: [] },
    optionsFilters: {},
  },

  effects: {
    // stock
    *GetUBSPosition({ payload, callback }, { call, put }) {
      const response = yield call(getUBSPosition, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // 添加盈亏幅度
        for (let i = 0; i < response.list.positionList.length; i++) {
          response.list.positionList[i].gainrate =
            response.list.positionList[i].stockprice / response.list.positionList[i].costprice - 1;
        }
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // option
    *GetFssFundOptionsList({ payload, callback }, { call, put }) {
      const response = yield call(getFssFundOptionsList, { groupId: payload.groupId });
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        let premium = 0;
        let sumprice = 0;
        let allpremium = 0;
        let remainpremium = 0;
        let allincome = 0;
        let thisincome = 0;
        if (payload.currentDataSource.length === 0) {
          for (let i = 0; i < response.data.length; i++) {
            if (response.data[i].name !== '期权合计') {
              premium += parseFloat(response.data[i].unkown2 || 0);
              sumprice += parseFloat(response.data[i].sumprice || 0);
              allpremium += parseFloat(response.data[i].allpremium || 0);
              remainpremium += parseFloat(response.data[i].remainpremium || 0);
              allincome += parseFloat(response.data[i].allincome || 0);
              thisincome += parseFloat(response.data[i].thisincome || 0);
            }
          }
        } else {
          for (let i = 0; i < payload.currentDataSource.length; i++) {
            if (payload.currentDataSource[i].name !== '期权合计') {
              premium += parseFloat(payload.currentDataSource[i].unkown2 || 0);
              sumprice += parseFloat(payload.currentDataSource[i].sumprice || 0);
              allpremium += parseFloat(payload.currentDataSource[i].allpremium || 0);
              remainpremium += parseFloat(payload.currentDataSource[i].remainpremium || 0);
              allincome += parseFloat(payload.currentDataSource[i].allincome || 0);
              thisincome += parseFloat(payload.currentDataSource[i].thisincome || 0);
            }
          }
        }

        response.data.unshift({
          name: '期权合计',
          unkown2: FormatnumberZero(premium, 2),
          sumprice: FormatnumberZero(sumprice, 2),
          allpremium: FormatnumberZero(allpremium, 2),
          remainpremium: FormatnumberZero(remainpremium, 2),
          allincome: FormatnumberZero(allincome, 2),
          thisincome: FormatnumberZero(thisincome, 2),
        });
        yield put({
          type: 'saveOptions',
          payload: {
            list: { code: 1, message: response.message, list: response.data },
            filter: {},
          } /* , pagination: {current: 1, pageSize: 30, total: response.data.length-1}, */,
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },

    saveOptions(state, action) {
      return {
        ...state,
        options: action.payload.list,
        optionsFilters: action.payload.filter,
      };
    },
  },
};
